unit fColecaoFilmes;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Imaging.jpeg,
  IniFiles, Data.DB, IBDatabase, IBCustomDataSet, IBQuery, Vcl.Samples.Spin, Vcl.Buttons,
  Vcl.ComCtrls, Vcl.ToolWin, Vcl.ActnList, Vcl.Imaging.pngimage;

const
  STotalPagina = 50;

type
  TF_ColecaoFilmes = class(TForm)
    pFiltro: TPanel;
    qFilmes: TIBQuery;
    ibTransaction: TIBTransaction;
    ibDatabase: TIBDatabase;
    edFiltroTitulo: TEdit;
    lbFiltroTitulo: TLabel;
    bvBottom: TBevel;
    bvTop: TBevel;
    lbFiltro: TLabel;
    bvRight: TBevel;
    lbFiltroGenero: TLabel;
    edFiltroGenero: TEdit;
    lbFiltroAno: TLabel;
    edFiltroAno: TSpinEdit;
    btnFiltrar: TBitBtn;
    pScrollBox: TScrollBox;
    lbStatus: TLabel;
    imgInf: TImage;
    pbProgresso: TProgressBar;
    imgPlanoFundo: TImage;
    imgTop: TImage;
    pPaginas: TPanel;
    Bevel1: TBevel;
    Bevel2: TBevel;
    Bevel3: TBevel;
    Image2: TImage;
    btnProximo: TBitBtn;
    btnAnterior: TBitBtn;
    lbPaginas: TLabel;
    pageLista: TPageScroller;
    pPag: TPanel;
    actList: TActionList;
    actProximo: TAction;
    actAnterior: TAction;
    Bevel4: TBevel;
    Bevel5: TBevel;
    Bevel6: TBevel;
    lbInformacoes: TLabel;
    lbInfGeral: TLabel;
    imgE: TImage;
    imgD: TImage;
    imgVisto: TImage;
    procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure actAnteriorExecute(Sender: TObject);
    procedure actProximoExecute(Sender: TObject);
    procedure actListUpdate(Action: TBasicAction; var Handled: Boolean);
    procedure btnFiltrarClick(Sender: TObject);
    procedure edFiltroTituloKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    FPaginas: integer;
    ftArea: TScrollBox;
    FPagina: integer;
    procedure AtualizaPaginas;
    procedure AtualizaPanelFiltro;
    procedure AtualizaFilmes(PID: integer);

    procedure lbPaginaClick(Sender: TObject);
    procedure FilmeDetalhes(Sender: TObject);
    procedure FilmeCadastro(Sender: TObject);
    procedure SetPagina(const Value: integer);
  public

  published
    property Pagina: integer read FPagina write SetPagina;
  end;

var
  F_ColecaoFilmes: TF_ColecaoFilmes;
  FImage: TBitmap;

implementation

{$R *.dfm}

uses fFilme_Detalhes, fCadastroFilmes;

procedure TF_ColecaoFilmes.actAnteriorExecute(Sender: TObject);
begin
  Pagina := Pagina - 1;
  AtualizaFilmes(actAnterior.Tag);
end;

procedure TF_ColecaoFilmes.actListUpdate(Action: TBasicAction;
  var Handled: Boolean);
begin
  actAnterior.Enabled := Pagina > 1;
  actProximo.Enabled := Pagina < FPaginas;
end;

procedure TF_ColecaoFilmes.actProximoExecute(Sender: TObject);
begin
  AtualizaFilmes(actProximo.Tag);
  Pagina := Pagina + 1;
end;

procedure TF_ColecaoFilmes.AtualizaFilmes(PID: integer);
var
  ftTemplate: TPanel;
  I,
    nL,
    nID,
    nLeft,
    nTop: integer;
begin
  if ftArea <> nil then
    ftArea.Free;

  ftArea := TScrollBox.Create(Self);
  ftArea.Visible := False;
  ftArea.ParentBackground := True;
  ftArea.BorderStyle := bsNone;
  ftArea.Align := alClient;
  ftArea.Parent := Self;
  ftArea.DoubleBuffered := True;
  ftArea.VertScrollBar.Tracking := True;
  ftArea.VertScrollBar.Style := ssFlat;
  nID := 1;
  nLeft := 8;
  nTop := 10;

  with qFilmes, Sql do
  try
    Close;
    Clear;
    Add('select first '+IntToStr(STotalPagina));
    Add('*');
    Add('from');
    Add('  BUSCA_FILME_LISTA(:TITULO, :GENERO, :ANO, :DVD) F');
    Add('where');
    Add('  F.ID >= :ID');
    ParamByName('TITULO').AsString := Trim(edFiltroTitulo.Text);
    ParamByName('GENERO').AsString := Trim(edFiltroGenero.Text);
    ParamByName('ANO').AsInteger := edFiltroAno.Value;
    ParamByName('DVD').AsInteger := 0;
    ParamByName('ID').AsInteger := PID;
    Open;
    if IsEmpty then
    begin
      ftTemplate := TPanel.Create(ftArea);
      ftTemplate.BevelKind := bkNone;
      ftTemplate.BevelOuter := bvNone;
      ftTemplate.Parent := ftArea;
      ftTemplate.DoubleBuffered := True;
      ftTemplate.Align := alClient;
      ftTemplate.Caption := 'Nenhum filme encontrado!';
      ftTemplate.Font.Name := 'Tahoma';
      ftTemplate.Font.Style := [fsBold];
      ftTemplate.Font.Color := clWhite;
      ftTemplate.Font.Size := 24;
      Exit;
    end;

    actAnterior.Tag := qFilmes.FieldByName('ID').AsInteger;
    Last;
    actProximo.Tag := Succ(qFilmes.FieldByName('ID').AsInteger);
    First;

    pScrollBox.BringToFront;
    lbStatus.Caption := 'Buscando lista de filmes... Aguarde...';
    pScrollBox.Visible := True;
    pbProgresso.Max := RecordCount;
    pbProgresso.Position := 0;
    while not Eof do
    begin
      pbProgresso.Position := Succ(pbProgresso.Position);
      Application.ProcessMessages;

      ftTemplate := TPanel.Create(ftArea);
      ftTemplate.BevelKind := bkNone;
      ftTemplate.BevelOuter := bvNone;
      ftTemplate.Parent := ftArea;
      ftTemplate.DoubleBuffered := True;
      ftTemplate.Top := nTop;
      ftTemplate.Left := nLeft;
      ftTemplate.Width := 450;
      ftTemplate.Height := 252;

      with TImage.Create(ftTemplate) do
      begin
        OnClick := FilmeDetalhes;
        Tag := FieldByName('ID_FILME_DADOS').AsInteger;
        Cursor := crHandPoint;
        Parent := ftTemplate;
        Top := 30;
        Left := 8;
        Width := 150;
        Height := 200;
        Stretch := True;
        TBlobField(qFilmes.FieldByName('CAPA')).SaveToFile(IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName))+'capa.jpg');
        Picture.LoadFromFile(IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName))+'capa.jpg');
      end;

      nL := 8;
      for I := 1 to 5 do
        with TImage.Create(ftTemplate) do
        begin
          Parent := ftTemplate;
          Top := 230;
          Left := nL;
          Width := 16;
          Height := 16;
          Stretch := True;
          nL := nL + 18;
          if FieldByName('NOTA').AsInteger >= I then
            Picture := imgE.Picture
          else
            Picture := imgD.Picture;
        end;

      if Boolean(FieldByName('VISTO').AsInteger) then
        with TImage.Create(ftTemplate) do
        begin
          Parent := ftTemplate;
          Top := 230;
          Left := 142;
          Width := 16;
          Height := 16;
          Stretch := True;
          Picture := imgVisto.Picture
        end;

      with TLabel.Create(ftTemplate) do
      begin
        OnClick := FilmeDetalhes;
        Tag := FieldByName('ID_FILME_DADOS').AsInteger;
        Cursor := crHandPoint;
        Parent := ftTemplate;
        Font.Style := [fsBold];
        Font.Color := clWhite;
        Font.Size := 12;
        Top := 3;
        Left := 8;
        Caption := Format('%s (%d)', [qFilmes.FieldByName('TITULO').AsString, qFilmes.FieldByName('ANO').AsInteger]);
      end;

      with TLabel.Create(ftTemplate) do
      begin
        OnDblClick := FilmeCadastro;
        Tag := FieldByName('ID_FILME_DADOS').AsInteger;
        Parent := ftTemplate;
        Font.Style := [fsItalic];
        Font.Color := clWhite;
        Font.Size := 10;
        Top := 30;
        Left := 164;
        Caption := qFilmes.FieldByName('TITULO_ORIGINAL').AsString+' ';
      end;

      with TLabel.Create(ftTemplate) do
      begin
        Parent := ftTemplate;
        Font.Style := [];
        Font.Color := clWhite;
        Font.Size := 10;
        Top := 55;
        Left := 164;
        Caption := qFilmes.FieldByName('GENERO').AsString;
      end;

      with TLabel.Create(ftTemplate) do
      begin
        Parent := ftTemplate;
        Font.Style := [];
        Font.Color := clWhite;
        Font.Size := 10;
        Top := 77;
        Left := 164;
        Caption := Format('%d min', [qFilmes.FieldByName('DURACAO').AsInteger]);
      end;

      with TLabel.Create(ftTemplate) do
      begin
        Name := 'sinopse'+FieldByName('ID_FILME_DADOS').AsString;
        AutoSize := False;
        Parent := ftTemplate;
        Font.Style := [];
        Font.Color := clWhite;
        Font.Size := 10;
        Top := 105;
        Left := 164;
        Width := 284;
        Height := 130;
        WordWrap := True;
        Caption := Format('Sinopse:'#13'%s', [qFilmes.FieldByName('SINOPSE').AsString]);
      end;

      Inc(nID);
      if not Odd(nID) then
      begin
        nLeft := 464;
      end
      else
      begin
        nLeft := 8;
        nTop := nTop + ftTemplate.Height + 6;
      end;

      Next;
    end;
  finally
    pScrollBox.Visible := False;
    ftArea.Visible := True;

    Close;
    Clear;
  end;
end;

procedure TF_ColecaoFilmes.AtualizaPaginas;
var
  I,
    nID,
    nLeft: integer;
  nPag: Double;
begin
  with qFilmes, Sql do
  try
    Close;
    Clear;
    Add('select count(*) TOTAL from BUSCA_FILME_LISTA(:TITULO, :GENERO, :ANO, :DVD) F');
    ParamByName('TITULO').AsString := Trim(edFiltroTitulo.Text);
    ParamByName('GENERO').AsString := Trim(edFiltroGenero.Text);
    ParamByName('ANO').AsInteger := edFiltroAno.Value;
    ParamByName('DVD').AsInteger := 0;
    Open;
    lbInfGeral.Caption := Format('Existem %d filmes listados.', [FieldByName('TOTAL').AsInteger]);

    nPag := FieldByName('TOTAL').AsInteger / STotalPagina;
    FPaginas := Round(Int(nPag));
    if Frac(nPag) > 0 then
      Inc(FPaginas);

    for I := Pred(pPag.ComponentCount) downto 0 do
      if pPag.Components[I] is TLabel then
        pPag.Components[I].Free;

    nID := 1;
    nLeft := 4;
    for I := 1 to FPaginas do
      with TLabel.Create(pPag) do
      begin
        Name := 'lb'+IntToStr(I);
        Tag := I;
        Cursor := crHandPoint;
        Font.Size := 12;
        Font.Color := clWhite;
        Font.Name := 'Tahoma';
        OnClick := lbPaginaClick;
        Parent := pPag;
        if I = FPaginas then
          Caption := Format('%d', [I])
        else
          Caption := Format('%d, ', [I]);
        Left := nLeft;
        if I < 10 then
          nLeft := nLeft + (Length(Caption) * 2) + 14
        else if I < 100 then
          nLeft := nLeft + (Length(Caption) * 3) + 14
        else if I < 1000 then
          nLeft := nLeft + (Length(Caption) * 4) + 14
        else if I < 10000 then
          nLeft := nLeft + (Length(Caption) * 5) + 14;
        pPag.Width := nLeft + 20;
        Inc(nID, STotalPagina);
      end;
    Pagina := 1;
  finally
    Close;
    Clear;
  end;
end;

procedure TF_ColecaoFilmes.AtualizaPanelFiltro;
begin
  pFiltro.Visible := True;
  pPaginas.Visible := True;
end;

procedure TF_ColecaoFilmes.btnFiltrarClick(Sender: TObject);
begin
  AtualizaPaginas;
  AtualizaFilmes(0);
end;

procedure TF_ColecaoFilmes.edFiltroTituloKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
    btnFiltrarClick(nil);
end;

procedure TF_ColecaoFilmes.FilmeCadastro(Sender: TObject);
begin
  with TF_CadastroFilmes.Create(Application) do
  try
    sdsDadosFilmes.Locate('ID_FILME_DADOS', TControl(Sender).Tag, []);
    ShowModal;
  finally
    Free;
  end;
end;

procedure TF_ColecaoFilmes.FilmeDetalhes(Sender: TObject);
var
  I: integer;
begin
  with TF_FilmeDetalhes.Create(Application) do
  try
    with Self.qFilmes, Sql do
    try
      Close;
      Clear;
      Add('select * from FILMES_DADOS where ID_FILME_DADOS = :ID_FILME_DADOS');
      ParamByName('ID_FILME_DADOS').AsInteger  := TControl(Sender).Tag;
      Open;

      lbTitulo.Caption := Format('%s (%d)', [FieldByName('TITULO').AsString, FieldByName('ANO').AsInteger]);;
      lbTituloOriginal.Caption := FieldByName('TITULO_ORIGINAL').AsString+' ';
      lbGenero.Caption := FieldByName('GENERO').AsString;
      lbDuracao.Caption := Format('%d min', [FieldByName('DURACAO').AsInteger]);
      lbSinopse.Caption := Format('Sinopse:'#13'%s', [FieldByName('SINOPSE').AsString]);

      TBlobField(FieldByName('CAPA')).SaveToFile(IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName))+'capa.jpg');
      imgCapa.Picture.LoadFromFile(IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName))+'capa.jpg');

      if FieldByName('NOTA').AsInteger >= 1 then
        imgN1.Picture := imgE.Picture
      else
        imgN1.Picture := imgD.Picture;

      if FieldByName('NOTA').AsInteger >= 2 then
        imgN2.Picture := imgE.Picture
      else
        imgN2.Picture := imgD.Picture;

      if FieldByName('NOTA').AsInteger >= 3 then
        imgN3.Picture := imgE.Picture
      else
        imgN3.Picture := imgD.Picture;

      if FieldByName('NOTA').AsInteger >= 4 then
        imgN4.Picture := imgE.Picture
      else
        imgN4.Picture := imgD.Picture;

      if FieldByName('NOTA').AsInteger >= 5 then
        imgN5.Picture := imgE.Picture
      else
        imgN5.Picture := imgD.Picture;

      imgVisto.Visible := Boolean(FieldByName('VISTO').AsInteger);

    finally
      Close;
      Clear;
    end;

    qFilmes.Close;
    qFilmes.ParamByName('ID_FILME_DADOS').AsInteger := TControl(Sender).Tag;
    qFilmes.Open;

    ShowModal;
  finally
    Free;
  end;
end;

procedure TF_ColecaoFilmes.FormCreate(Sender: TObject);
var
  iniFile: TIniFile;
begin
  if Screen.Height < Height then
    Height := Screen.Height - 40;

  with ibDatabase do
  begin
    Close;
    iniFile := TIniFile.Create('.\4filmes.ini');
    try
      DatabaseName := iniFile.ReadString('4filmes', 'database', '');
    finally
      iniFile.Free;
    end;
    Open;
  end;

  AtualizaPanelFiltro;
end;

procedure TF_ColecaoFilmes.FormMouseWheelDown(Sender: TObject;
  Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
  with ftArea.VertScrollBar Do
  begin
   if (Position <= (Range - Increment)) then
     Position := Position + Increment
   else
     Position := Range - Increment;
  end;
end;

procedure TF_ColecaoFilmes.FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
  MousePos: TPoint; var Handled: Boolean);
begin
  with ftArea.VertScrollBar do
  begin
    if (Position >= Increment) then
      Position := Position - Increment
    else
      Position := 0;
  end;
end;

procedure TF_ColecaoFilmes.FormShow(Sender: TObject);
begin
  AtualizaPaginas;
  AtualizaFilmes(0);
end;

procedure TF_ColecaoFilmes.lbPaginaClick(Sender: TObject);
begin
  Pagina := TLabel(Sender).Tag;
  AtualizaFilmes((TLabel(Sender).Tag * STotalPagina) - Pred((STotalPagina)));
end;

procedure TF_ColecaoFilmes.SetPagina(const Value: integer);
var
  nLB: TLabel;
begin
  nLB := TLabel(pPag.FindComponent('lb'+IntToStr(FPagina)));
  if nLB <> nil then
  begin
    nLB.Font.Style := [];
    nLB.Font.Color := clWhite;
    nLB.Cursor := crHandPoint;
    nLB.OnClick := lbPaginaClick;
  end;

  FPagina := Value;

  nLB := TLabel(pPag.FindComponent('lb'+IntToStr(FPagina)));
  if nLB <> nil then
  begin
    nLB.Font.Style := [fsBold];
    nLB.Font.Color := clYellow;
    nLB.Cursor := crDefault;
    nLB.OnClick := nil;
    actAnterior.Tag := (nLB.Tag * STotalPagina) - Pred((STotalPagina));
    actProximo.Tag := (nLB.Tag * STotalPagina);
  end;
end;

end.
